# @shuaige : 陈世玉
# @name :save_csv.py
# @time :2024/12/5 21:16
import csv
import re

pattern = re.compile(
    r"【一般资料】 (?P<性别>[^\s]+)，(?P<年龄>\d+)岁?，?(?P<职业>[^\s]+)?\s+"
    r"【主诉】 (?P<主诉>[^【]+)\s+"
    r"【现病史】 (?P<现病史>[^【]+)"
)

# 读取数据文件
with open('data.txt', 'r', encoding='utf-8') as file:
    data = file.read()

# 查找所有匹配的数据
matches = pattern.findall(data)

# 将匹配结果转换为字典列表
cases = []
for match in matches:
    case = {
        "性别": match[0],
        "年龄": int(match[1]),
        "职业": match[2] if match[2] else "-",
        "主诉": match[3].strip(),
        "现病史": match[4].strip()
    }
    cases.append(case)

# 写入CSV文件
with open('data.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
    fieldnames = ['性别', '年龄', '职业', '主诉', '现病史']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for case in cases:
        writer.writerow(case)

print("数据已成功解析并保存为 CSV 文件。")
